package org.daymr.puzzle.renderer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import org.daymr.puzzle.entity.Desk;
import org.daymr.puzzle.tools.MatrixUtils;
import org.daymr.puzzle.view.GameView;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

/**
 * 游戏渲染类
 * @author Ben
 * @date 2014年12月19日 下午11:09:14
 */
public class PuzzleRenderer implements GLSurfaceView.Renderer{
	public GameView mGameView;
	/**框架*/
	public Desk mDesk;

	public PuzzleRenderer(GameView gv){
		this.mGameView = gv;
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		//创建对象 
        mDesk = new Desk(mGameView);
        
        //初始化变换矩阵
        MatrixUtils.setInitMatrix();
        
		//设置屏幕背景色RGBA
        GLES20.glClearColor(0f,0f,0f, 1.0f);  
        //打开深度检测
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        //关闭背面剪裁   
        GLES20.glDisable(GLES20.GL_CULL_FACE);
       
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		 //设置视窗大小及位置 
    	GLES20.glViewport(0, 0, width, height); 
    	//计算GLSurfaceView的宽高比
        float ratio = (float) width / height;
        //调用此方法计算产生透视投影矩阵
        MatrixUtils.setProjectFrustum(-ratio, ratio, -1, 1, 1, 10);
       //调用此方法产生摄像机9参数位置矩阵
        MatrixUtils.setCamera(-2f,2f,2f,0f,0f,0f,0f,1f,0f);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		//清除深度缓冲与颜色缓冲
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        //绘画
        mDesk.draw();
	}
	
	

}
